package cn.crex1.xiaomi.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.crex1.xiaomi.service.CategorySService;
import cn.crex1.xiaomi.service.SearchService;
import cn.crex1.xiaomi.service.impl.CategorySServiceImpl;
import cn.crex1.xiaomi.service.impl.SearchServiceImpl;
import cn.crex1.xiaomi.vo.ProductVO;

@WebServlet("/SearchServlet/*")
public class SearchController extends HttpServlet {
	
	private SearchService searchService=new SearchServiceImpl(); 
	private CategorySService categorySService=new CategorySServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String q[] = req.getRequestURI().split("/");
		System.out.println(q[3]);
		if (q[3].equals("PreviousPage")) {
			PreviousPage(req,resp);
		}else if(q[3].equals("HomePage")) {
			HomePage(req,resp);
		}else if(q[3].equals("NextPage")) {
			NextPage(req,resp);
		}
		
	}

	private void PreviousPage(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, JsonGenerationException, JsonMappingException {
		resp.setContentType("text/html;charset=utf-8");
		String namess=req.getParameter("search");
		String pagenumbers=req.getParameter("pagenumber");
		int pagenumber=Integer.valueOf(pagenumbers);
		ProductVO productVO=new ProductVO();
		
		if(namess.equals("小米手机")) {
			productVO.setCategory_id(2);
			
			List<ProductVO> products=categorySService.selectOProductByOCategoryName(productVO);
			int counts=products.size();
			int	pages=((counts-1)/10)+1;//判断多少页
			System.out.println("总页数"+pages);
			
			Map<String, List<Object>> map=new HashMap<String, List<Object>>();
			List<Object> list=new ArrayList<Object>();
			List<Object> lists=new ArrayList<Object>();
			
			for(int i=0;i<counts;i++) {
				if(i>=pagenumber*10-10-10&&i<pagenumber*10-10){
				
				List<Object> list11=new ArrayList<Object>();
				String name=products.get(i).getName();
				double price=products.get(i).getShow_price();
				String introduce=products.get(i).getIntroduce();
				String img=products.get(i).getPic_img();
				String[] listimg=img.split("/");
				String subimg=listimg[0]+"/"+listimg[2]+"/"+listimg[3];
				
				long id=products.get(i).getProduct_number();
				list11.add(name);
				list11.add(price);
				list11.add(introduce);
				list11.add(subimg);
				list11.add(id);
				list.add(list11);
			}
			}
			lists.add(pages);
			lists.add(pagenumber-1);
			map.put("state", lists);
			map.put("state1", list);
				ObjectMapper om = new ObjectMapper();
				om.writeValue(resp.getWriter(), map);
		}else if(namess.equals("平板·笔记本")) {
			productVO.setCategory_id(4);
			
			List<ProductVO> products=categorySService.selectOProductByOCategoryName(productVO);
			int counts=products.size();
			int	pages=((counts-1)/10)+1;//判断多少页
			System.out.println("总页数"+pages);
			
			Map<String, List<Object>> map=new HashMap<String, List<Object>>();
			List<Object> list=new ArrayList<Object>();
			List<Object> lists=new ArrayList<Object>();
			
			for(int i=0;i<counts;i++) {
				if(i>=pagenumber*10-10-10&&i<pagenumber*10-10){
				
				List<Object> list11=new ArrayList<Object>();
				String name=products.get(i).getName();
				double price=products.get(i).getShow_price();
				String introduce=products.get(i).getIntroduce();
				String img=products.get(i).getPic_img();
				String[] listimg=img.split("/");
				String subimg=listimg[0]+"/"+listimg[2]+"/"+listimg[3];
				
				long id=products.get(i).getProduct_number();
				list11.add(name);
				list11.add(price);
				list11.add(introduce);
				list11.add(subimg);
				list11.add(id);
				list.add(list11);
			}
			}
			lists.add(pages);
			lists.add(pagenumber-1);
			System.out.println(lists);
			map.put("state", lists);
			map.put("state1", list);
				ObjectMapper om = new ObjectMapper();
				om.writeValue(resp.getWriter(), map);
		}else if(namess.equals("电视")) {
			productVO.setCategory_id(6);
			
			List<ProductVO> products=categorySService.selectOProductByOCategoryName(productVO);
			int counts=products.size();
			int	pages=((counts-1)/10)+1;//判断多少页
			System.out.println("总页数"+pages);
			
			Map<String, List<Object>> map=new HashMap<String, List<Object>>();
			List<Object> list=new ArrayList<Object>();
			List<Object> lists=new ArrayList<Object>();
			
			for(int i=0;i<counts;i++) {
				if(i>=pagenumber*10-10-10&&i<pagenumber*10-10){
				
				List<Object> list11=new ArrayList<Object>();
				String name=products.get(i).getName();
				double price=products.get(i).getShow_price();
				String introduce=products.get(i).getIntroduce();
				String img=products.get(i).getPic_img();
				String[] listimg=img.split("/");
				String subimg=listimg[0]+"/"+listimg[2]+"/"+listimg[3];
				
				long id=products.get(i).getProduct_number();
				list11.add(name);
				list11.add(price);
				list11.add(introduce);
				list11.add(subimg);
				list11.add(id);
				list.add(list11);
			}
			}
			lists.add(pages);
			lists.add(pagenumber-1);
			System.out.println(lists);
			map.put("state", lists);
			map.put("state1", list);
				ObjectMapper om = new ObjectMapper();
				om.writeValue(resp.getWriter(), map);
		}else if(namess.equals("盒子·影音")) {
			productVO.setCategory_id(9);
			
			List<ProductVO> products=categorySService.selectOProductByOCategoryName(productVO);
			int counts=products.size();
			int	pages=((counts-1)/10)+1;//判断多少页
			System.out.println("总页数"+pages);
			
			Map<String, List<Object>> map=new HashMap<String, List<Object>>();
			List<Object> list=new ArrayList<Object>();
			List<Object> lists=new ArrayList<Object>();
			
			for(int i=0;i<counts;i++) {
				if(i>=pagenumber*10-10-10&&i<pagenumber*10-10){
				
				List<Object> list11=new ArrayList<Object>();
				String name=products.get(i).getName();
				double price=products.get(i).getShow_price();
				String introduce=products.get(i).getIntroduce();
				String img=products.get(i).getPic_img();
				String[] listimg=img.split("/");
				String subimg=listimg[0]+"/"+listimg[2]+"/"+listimg[3];
				
				long id=products.get(i).getProduct_number();
				list11.add(name);
				list11.add(price);
				list11.add(introduce);
				list11.add(subimg);
				list11.add(id);
				list.add(list11);
			}
			}
			lists.add(pages);
			lists.add(pagenumber-1);
			map.put("state", lists);
			map.put("state1", list);
				ObjectMapper om = new ObjectMapper();
				om.writeValue(resp.getWriter(), map);
		}else if(namess.equals("路由器")) {
			productVO.setCategory_id(3);
			
			List<ProductVO> products=categorySService.selectOProductByOCategoryName(productVO);
			int counts=products.size();
			int	pages=((counts-1)/10)+1;//判断多少页
			System.out.println("总页数"+pages);
			
			Map<String, List<Object>> map=new HashMap<String, List<Object>>();
			List<Object> list=new ArrayList<Object>();
			List<Object> lists=new ArrayList<Object>();
			
			for(int i=0;i<counts;i++) {
				if(i>=pagenumber*10-10-10&&i<pagenumber*10-10){
				
				List<Object> list11=new ArrayList<Object>();
				String name=products.get(i).getName();
				double price=products.get(i).getShow_price();
				String introduce=products.get(i).getIntroduce();
				String img=products.get(i).getPic_img();
				String[] listimg=img.split("/");
				String subimg=listimg[0]+"/"+listimg[2]+"/"+listimg[3];
				
				long id=products.get(i).getProduct_number();
				list11.add(name);
				list11.add(price);
				list11.add(introduce);
				list11.add(subimg);
				list11.add(id);
				list.add(list11);
			}
			}
			lists.add(pages);
			lists.add(pagenumber-1);
			map.put("state", lists);
			map.put("state1", list);
				ObjectMapper om = new ObjectMapper();
				om.writeValue(resp.getWriter(), map);
		}else if(namess.equals("智能硬件")) {
			productVO.setCategory_id(5);
			List<ProductVO> products=categorySService.selectOProductByOCategoryName(productVO);
			int counts=products.size();
			int	pages=((counts-1)/10)+1;//判断多少页
			System.out.println("总页数"+pages);
			
			Map<String, List<Object>> map=new HashMap<String, List<Object>>();
			List<Object> list=new ArrayList<Object>();
			List<Object> lists=new ArrayList<Object>();
			
			for(int i=0;i<counts;i++) {
				if(i>=pagenumber*10-10-10&&i<pagenumber*10-10){
				
				List<Object> list11=new ArrayList<Object>();
				String name=products.get(i).getName();
				double price=products.get(i).getShow_price();
				String introduce=products.get(i).getIntroduce();
				String img=products.get(i).getPic_img();
				String[] listimg=img.split("/");
				String subimg=listimg[0]+"/"+listimg[2]+"/"+listimg[3];
				
				long id=products.get(i).getProduct_number();
				list11.add(name);
				list11.add(price);
				list11.add(introduce);
				list11.add(subimg);
				list11.add(id);
				list.add(list11);
			}
			}
			lists.add(pages);
			lists.add(pagenumber-1);
			map.put("state", lists);
			map.put("state1", list);
				ObjectMapper om = new ObjectMapper();
				om.writeValue(resp.getWriter(), map);
		}else {
		
		String[] names=namess.split("相");
		productVO.setName(names[0]);
		
	
		List<ProductVO> products=searchService.selectOProductByOProductName(productVO);
		int counts=products.size();
		int	pages=((counts-1)/10)+1;//判断多少页
		System.out.println("总页数"+pages);
		
		Map<String, List<Object>> map=new HashMap<String, List<Object>>();
		List<Object> list=new ArrayList<Object>();
		List<Object> lists=new ArrayList<Object>();
		
		for(int i=0;i<counts;i++) {
			
			if(i>=pagenumber*10-10-10&&i<pagenumber*10-10){
			List<Object> list11=new ArrayList<Object>();
			String name=products.get(i).getName();
			double price=products.get(i).getShow_price();
			String introduce=products.get(i).getIntroduce();
			String img=products.get(i).getPic_img();
			String[] listimg=img.split("/");
			String subimg=listimg[0]+"/"+listimg[2]+"/"+listimg[3];
			
			long id=products.get(i).getProduct_number();
			list11.add(name);
			list11.add(price);
			list11.add(introduce);
			list11.add(subimg);
			list11.add(id);
			list.add(list11);
			System.out.println(list);
			}
		}
		lists.add(pages);
		lists.add(pagenumber-1);
		System.out.println(lists);
		map.put("state", lists);
		map.put("state1", list);
			ObjectMapper om = new ObjectMapper();
			om.writeValue(resp.getWriter(), map);
	}
	}

	private void HomePage(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, JsonGenerationException, JsonMappingException {
		resp.setContentType("text/html;charset=utf-8");
		String names=req.getParameter("search");
		ProductVO productVO=new ProductVO();
		productVO.setName(names);
	
		List<ProductVO> products=searchService.selectOProductByOProductName(productVO);
		int counts=products.size();
		int	pages=((counts-1)/10)+1;//判断多少页
		System.out.println("总页数"+pages);
		
		Map<String, List<Object>> map=new HashMap<String, List<Object>>();
		List<Object> list=new ArrayList<Object>();
		List<Object> lists=new ArrayList<Object>();
		
		for(int i=0;i<counts;i++) {
			if(i<10) {
			List<Object> list11=new ArrayList<Object>();
			String name=products.get(i).getName();
			double price=products.get(i).getShow_price();
			String introduce=products.get(i).getIntroduce();
			String img=products.get(i).getPic_img();
			String[] listimg=img.split("/");
			String subimg=listimg[0]+"/"+listimg[2]+"/"+listimg[3];
			
			long id=products.get(i).getProduct_number();
			list11.add(name);
			list11.add(price);
			list11.add(introduce);
			list11.add(subimg);
			list11.add(id);
			list.add(list11);
			System.out.println(list);
			}
		}
		lists.add(pages);
		System.out.println(lists);
		map.put("state", lists);
		map.put("state1", list);
			ObjectMapper om = new ObjectMapper();
			om.writeValue(resp.getWriter(), map);
	}

	private void NextPage(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, JsonGenerationException, JsonMappingException {
		resp.setContentType("text/html;charset=utf-8");
		String namess=req.getParameter("search");
		String pagenumbers=req.getParameter("pagenumber");
		int pagenumber=Integer.valueOf(pagenumbers);
		ProductVO productVO=new ProductVO();
		
		if(namess.equals("小米手机")) {
			productVO.setCategory_id(2);
			
			List<ProductVO> products=categorySService.selectOProductByOCategoryName(productVO);
			int counts=products.size();
			int	pages=((counts-1)/10)+1;//判断多少页
			System.out.println("总页数"+pages);
			
			Map<String, List<Object>> map=new HashMap<String, List<Object>>();
			List<Object> list=new ArrayList<Object>();
			List<Object> lists=new ArrayList<Object>();
			
			for(int i=0;i<counts;i++) {
				if(i>=pagenumber*10&&i<pagenumber*10+10) {
				
				List<Object> list11=new ArrayList<Object>();
				String name=products.get(i).getName();
				double price=products.get(i).getShow_price();
				String introduce=products.get(i).getIntroduce();
				String img=products.get(i).getPic_img();
				String[] listimg=img.split("/");
				String subimg=listimg[0]+"/"+listimg[2]+"/"+listimg[3];
				
				long id=products.get(i).getProduct_number();
				list11.add(name);
				list11.add(price);
				list11.add(introduce);
				list11.add(subimg);
				list11.add(id);
				list.add(list11);
				System.out.println("下一页数据"+list);
			}
			}
			lists.add(pages);
			lists.add(pagenumber+1);
			System.out.println(lists);
			map.put("state", lists);
			map.put("state1", list);
				ObjectMapper om = new ObjectMapper();
				om.writeValue(resp.getWriter(), map);
		}else if(namess.equals("平板·笔记本")) {
			productVO.setCategory_id(4);
			
			List<ProductVO> products=categorySService.selectOProductByOCategoryName(productVO);
			int counts=products.size();
			int	pages=((counts-1)/10)+1;//判断多少页
			System.out.println("总页数"+pages);
			
			Map<String, List<Object>> map=new HashMap<String, List<Object>>();
			List<Object> list=new ArrayList<Object>();
			List<Object> lists=new ArrayList<Object>();
			
			for(int i=0;i<counts;i++) {
				if(i>=pagenumber*10&&i<pagenumber*10+10) {
				
				List<Object> list11=new ArrayList<Object>();
				String name=products.get(i).getName();
				double price=products.get(i).getShow_price();
				String introduce=products.get(i).getIntroduce();
				String img=products.get(i).getPic_img();
				String[] listimg=img.split("/");
				String subimg=listimg[0]+"/"+listimg[2]+"/"+listimg[3];
				
				long id=products.get(i).getProduct_number();
				list11.add(name);
				list11.add(price);
				list11.add(introduce);
				list11.add(subimg);
				list11.add(id);
				list.add(list11);
				System.out.println("下一页数据"+list);
			}
			}
			lists.add(pages);
			lists.add(pagenumber+1);
			System.out.println(lists);
			map.put("state", lists);
			map.put("state1", list);
				ObjectMapper om = new ObjectMapper();
				om.writeValue(resp.getWriter(), map);
		}else if(namess.equals("电视")) {
			productVO.setCategory_id(6);
			
			List<ProductVO> products=categorySService.selectOProductByOCategoryName(productVO);
			int counts=products.size();
			int	pages=((counts-1)/10)+1;//判断多少页
			System.out.println("总页数"+pages);
			
			Map<String, List<Object>> map=new HashMap<String, List<Object>>();
			List<Object> list=new ArrayList<Object>();
			List<Object> lists=new ArrayList<Object>();
			
			for(int i=0;i<counts;i++) {
				if(i>=pagenumber*10&&i<pagenumber*10+10) {
				
				List<Object> list11=new ArrayList<Object>();
				String name=products.get(i).getName();
				double price=products.get(i).getShow_price();
				String introduce=products.get(i).getIntroduce();
				String img=products.get(i).getPic_img();
				String[] listimg=img.split("/");
				String subimg=listimg[0]+"/"+listimg[2]+"/"+listimg[3];
				
				long id=products.get(i).getProduct_number();
				list11.add(name);
				list11.add(price);
				list11.add(introduce);
				list11.add(subimg);
				list11.add(id);
				list.add(list11);
				System.out.println("下一页数据"+list);
			}
			}
			lists.add(pages);
			lists.add(pagenumber+1);
			System.out.println(lists);
			map.put("state", lists);
			map.put("state1", list);
				ObjectMapper om = new ObjectMapper();
				om.writeValue(resp.getWriter(), map);
		}else if(namess.equals("盒子·影音")) {
			productVO.setCategory_id(9);
			
			List<ProductVO> products=categorySService.selectOProductByOCategoryName(productVO);
			int counts=products.size();
			int	pages=((counts-1)/10)+1;//判断多少页
			System.out.println("总页数"+pages);
			
			Map<String, List<Object>> map=new HashMap<String, List<Object>>();
			List<Object> list=new ArrayList<Object>();
			List<Object> lists=new ArrayList<Object>();
			
			for(int i=0;i<counts;i++) {
				if(i>=pagenumber*10&&i<pagenumber*10+10) {
				
				List<Object> list11=new ArrayList<Object>();
				String name=products.get(i).getName();
				double price=products.get(i).getShow_price();
				String introduce=products.get(i).getIntroduce();
				String img=products.get(i).getPic_img();
				String[] listimg=img.split("/");
				String subimg=listimg[0]+"/"+listimg[2]+"/"+listimg[3];
				
				long id=products.get(i).getProduct_number();
				list11.add(name);
				list11.add(price);
				list11.add(introduce);
				list11.add(subimg);
				list11.add(id);
				list.add(list11);
				System.out.println("下一页数据"+list);
			}
			}
			lists.add(pages);
			lists.add(pagenumber+1);
			System.out.println(lists);
			map.put("state", lists);
			map.put("state1", list);
				ObjectMapper om = new ObjectMapper();
				om.writeValue(resp.getWriter(), map);
		}else if(namess.equals("路由器")) {
			productVO.setCategory_id(3);
			
			List<ProductVO> products=categorySService.selectOProductByOCategoryName(productVO);
			int counts=products.size();
			int	pages=((counts-1)/10)+1;//判断多少页
			System.out.println("总页数"+pages);
			
			Map<String, List<Object>> map=new HashMap<String, List<Object>>();
			List<Object> list=new ArrayList<Object>();
			List<Object> lists=new ArrayList<Object>();
			
			for(int i=0;i<counts;i++) {
				if(i>=pagenumber*10&&i<pagenumber*10+10) {
				
				List<Object> list11=new ArrayList<Object>();
				String name=products.get(i).getName();
				double price=products.get(i).getShow_price();
				String introduce=products.get(i).getIntroduce();
				String img=products.get(i).getPic_img();
				String[] listimg=img.split("/");
				String subimg=listimg[0]+"/"+listimg[2]+"/"+listimg[3];
				
				long id=products.get(i).getProduct_number();
				list11.add(name);
				list11.add(price);
				list11.add(introduce);
				list11.add(subimg);
				list11.add(id);
				list.add(list11);
				System.out.println("下一页数据"+list);
			}
			}
			lists.add(pages);
			lists.add(pagenumber+1);
			System.out.println(lists);
			map.put("state", lists);
			map.put("state1", list);
				ObjectMapper om = new ObjectMapper();
				om.writeValue(resp.getWriter(), map);
		}else if(namess.equals("智能硬件")) {
			productVO.setCategory_id(5);
			List<ProductVO> products=categorySService.selectOProductByOCategoryName(productVO);
			int counts=products.size();
			int	pages=((counts-1)/10)+1;//判断多少页
			System.out.println("总页数"+pages);
			
			Map<String, List<Object>> map=new HashMap<String, List<Object>>();
			List<Object> list=new ArrayList<Object>();
			List<Object> lists=new ArrayList<Object>();
			
			for(int i=0;i<counts;i++) {
				if(i>=pagenumber*10&&i<pagenumber*10+10) {
				
				List<Object> list11=new ArrayList<Object>();
				String name=products.get(i).getName();
				double price=products.get(i).getShow_price();
				String introduce=products.get(i).getIntroduce();
				String img=products.get(i).getPic_img();
				String[] listimg=img.split("/");
				String subimg=listimg[0]+"/"+listimg[2]+"/"+listimg[3];
				
				long id=products.get(i).getProduct_number();
				list11.add(name);
				list11.add(price);
				list11.add(introduce);
				list11.add(subimg);
				list11.add(id);
				list.add(list11);
				System.out.println("下一页数据"+list);
			}
			}
			lists.add(pages);
			lists.add(pagenumber+1);
			System.out.println(lists);
			map.put("state", lists);
			map.put("state1", list);
				ObjectMapper om = new ObjectMapper();
				om.writeValue(resp.getWriter(), map);
		}else {
			String[] names=namess.split("相");
			productVO.setName(names[0]);
			
			List<ProductVO> products=searchService.selectOProductByOProductName(productVO);
			int counts=products.size();
			int	pages=((counts-1)/10)+1;//判断多少页
			System.out.println("总页数"+pages);
			
			Map<String, List<Object>> map=new HashMap<String, List<Object>>();
			List<Object> list=new ArrayList<Object>();
			List<Object> lists=new ArrayList<Object>();
			
			for(int i=0;i<counts;i++) {
				if(i>=pagenumber*10&&i<pagenumber*10+10) {
				
				List<Object> list11=new ArrayList<Object>();
				String name=products.get(i).getName();
				double price=products.get(i).getShow_price();
				String introduce=products.get(i).getIntroduce();
				String img=products.get(i).getPic_img();
				String[] listimg=img.split("/");
				String subimg=listimg[0]+"/"+listimg[2]+"/"+listimg[3];
				
				long id=products.get(i).getProduct_number();
				list11.add(name);
				list11.add(price);
				list11.add(introduce);
				list11.add(subimg);
				list11.add(id);
				list.add(list11);
				System.out.println("下一页数据"+list);
			}
			}
			lists.add(pages);
			lists.add(pagenumber+1);
			System.out.println(lists);
			map.put("state", lists);
			map.put("state1", list);
				ObjectMapper om = new ObjectMapper();
				om.writeValue(resp.getWriter(), map);
		}
		
		List<ProductVO> products=categorySService.selectOProductByOCategoryName(productVO);
		int counts=products.size();
		int	pages=((counts-1)/10)+1;//判断多少页
		System.out.println("总页数"+pages);
		
		Map<String, List<Object>> map=new HashMap<String, List<Object>>();
		List<Object> list=new ArrayList<Object>();
		List<Object> lists=new ArrayList<Object>();
		
		for(int i=0;i<counts;i++) {
			if(i>=pagenumber*10&&i<pagenumber*10+10) {
			
			List<Object> list11=new ArrayList<Object>();
			String name=products.get(i).getName();
			double price=products.get(i).getShow_price();
			String introduce=products.get(i).getIntroduce();
			String img=products.get(i).getPic_img();
			String[] listimg=img.split("/");
			String subimg=listimg[0]+"/"+listimg[2]+"/"+listimg[3];
			
			long id=products.get(i).getProduct_number();
			list11.add(name);
			list11.add(price);
			list11.add(introduce);
			list11.add(subimg);
			list11.add(id);
			list.add(list11);
			System.out.println("下一页数据"+list);
		}
		}
		lists.add(pages);
		lists.add(pagenumber+1);
		System.out.println(lists);
		map.put("state", lists);
		map.put("state1", list);
			ObjectMapper om = new ObjectMapper();
			om.writeValue(resp.getWriter(), map);
		
	}
}
